﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Fireball.Syntax.Parser
{
    public sealed class KeywordsPattern : Pattern
    {
        private List<string> _Keywords = null;
        private Regex _Regex = null;

        public void Prepare()
        {
            if (_Regex == null)
            {
                string[] keys = this.Text.Replace("\n", " ").Replace("\r", "").Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                //List<string> _Keywords = new List<string>(keys);

                StringBuilder sbRegex = new StringBuilder();
                sbRegex.Append(@"\b(?<KEYWORD>");
                for (int i = 0; i < keys.Length; i++)
                {
                    sbRegex.Append(keys[i]);

                    if (i < keys.Length - 1)
                        sbRegex.Append("|");
                }
                sbRegex.Append(@")\b");

                _Regex = new Regex(sbRegex.ToString(), RegexOptions.Compiled);
            }

        }

        public override PatterFindResult FindIn(string line, int startPos)
        {
            if (this._Regex == null)
                this.Prepare();

            var m = _Regex.Match(line, startPos);
            if (m.Success)
            {
                return new PatterFindResult
                {
                    Text = m.Value,
                    StartPos = m.Index,
                    EndPos = m.Index + m.Length
                };
            }
            return PatterFindResult.Empty;

            /*int index = line.Length;
            string currentResult = null;

            foreach (string c in _Keywords)
            {
                int tmp = line.IndexOf(c, startPos);
                if (tmp == -1) continue;
                if (tmp == startPos)
                {
                    if (IsValidKey(line, tmp, c))
                    {
                        index = tmp;
                        currentResult = c;
                        break;
                    }
                }
                else if (IsValidKey(line, tmp, c))
                {
                    if (tmp < index)
                    {
                        index = tmp;
                        currentResult = c;
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(currentResult))
                return null;

            return new PatterFindResult
            {
                EndPos = index + currentResult.Length,
                Text = currentResult,
                StartPos = index
            };*/
        }
        //TODO usare regex
        private bool IsValidKey(string line, int index, string key)
        {
            if (index != 0)
            {
                string pc = line.Substring(index - 1,1);
                if (!string.IsNullOrEmpty(pc))
                    if (pc != "\t")
                        return false;
            }


            int end = index + key.Length;

            if (end < line.Length)
            {
                string ec = line.Substring(end + 1, 1);

                if (!string.IsNullOrEmpty(ec))
                    if (ec != " ")// non controllo x \n xke e' rimosso all inizio
                        return false;
            }

            return true;

        }
    }
}
